package ink.lovejinhu.swardToOffer;

import ink.lovejinhu.common.TreeNode;

public class Offer_026 {
    /**
     * 判读B是不是A的子结构
     *
     * @param A
     * @param B
     * @return
     */
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B == null || A == null) {
            return false;
        }
        dfs01(A, B);
        return flag;
    }

    boolean flag = false;

    public void dfs01(TreeNode A, TreeNode B) {
        if (A == null) {
            return;
        }
        boolean dfs = dfs(A, B);
        if (dfs == true) {
            flag = true;
            return;
        } else {
            dfs01(A.left, B);
            dfs01(A.right, B);
        }

    }

    public boolean dfs(TreeNode A, TreeNode B) {
        if (A == null && B == null) {
            return true;
        } else if (A != null && B != null) {
            if (A.val != B.val) {
                return false;
            }
            return dfs(A.left, B.left) && dfs(A.right, B.right);
        } else if (A != null && B == null) {
            return true;
        } else {
            return false;
        }

    }
}
